package com.zHan.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zHan.sky.common.contant.MessageConstant;
import com.zHan.sky.common.exception.DeletionNotAllowedException;
import com.zHan.sky.dto.SetmealDto;
import com.zHan.sky.entity.Setmeal;
import com.zHan.sky.entity.SetmealDish;
import com.zHan.sky.mapper.SetmealMapper;
import com.zHan.sky.service.SetmealDishService;
import com.zHan.sky.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐和对应的菜品
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //新增套餐的基础信息
        this.save(setmealDto);

        //获得套餐id
        Long setmealId = setmealDto.getId();

        //保存菜品到对应的表
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.forEach((item)->{
            item.setSetmealId(setmealId);
        });

        //保存菜品到dish_flavor表
        setmealDishService.saveBatch(setmealDishList);
    }



    /**
     * 删除套餐及对应的表
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //查询菜品的状态是否停售
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);

        //不停售，抛出业务无异常
        if (this.count(setmealLambdaQueryWrapper)>0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        //停售，删除基础信息
        this.removeByIds(ids);

        //再删除对应的表信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    /**
     * 修改套餐及对应的表
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐表的基础信息，操作setmeal表
        this.updateById(setmealDto);

        //修改对应的表信息，操作setmeal_dish表
        //清空setmeal_dish表
        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(queryWrapper);

        //新增setmeal_dish表的信息
        //保存菜品到对应的表
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.forEach((item)->{
            item.setSetmealId(setmealId);
        });

        //保存菜品到dish_flavor表
        setmealDishService.saveBatch(setmealDishList);
    }

    /**
     * 根据id查询套餐及菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐的基本信息，setmeal表
        Setmeal setmeal = this.getById(id);

        //创建setmealDto对象
        SetmealDto setmealDto=new SetmealDto();
        //对象拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询套餐对应的表信息，setmeal_dish表
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    /**
     * 得到各个状态的套餐数量
     * @param status
     * @return
     */
    @Override
    public Integer getStatusCount(Integer status) {
        return lambdaQuery()
                .eq(status != null, Setmeal::getStatus, status).count();
    }
}
